package com.rfid.assistant.base;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;

import androidx.annotation.Nullable;


public abstract class BaseActivity<T extends BasePresenter>  extends BaseTitleActivity {

    protected T mPresenter;
    CountDownTimer mCountDownTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getPresenter();
        if (mPresenter != null)
            mPresenter.onAttach(this);


        int time = getCountDownTime();
        if (time > 0) {
            mCountDownTimer = new CountDownTimer(time, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    onTimeTick(millisUntilFinished);
                }

                @Override
                public void onFinish() {
                    onTimeFinish();
                }
            };
            mCountDownTimer.start();
        }
    }

    protected abstract int getCountDownTime();

    protected void onTimeTick(long millisUntilFinished){

    }

    protected void onTimeFinish() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        setSystemUIVisible(false);
    }

    protected void setSystemUIVisible(boolean show) {
//        if (true) {
//            return;
//        }
        if (show) {
            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            uiFlags |= 0x00001000;
            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
        } else {
            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            uiFlags |= 0x00001000;
            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
        }
    }

    private boolean isResetCDTime() {
        return true;
    }

    protected void resetCountDownTime(){
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    onTimeTick(millisUntilFinished);
                }

                @Override
                public void onFinish() {
                    onTimeFinish();
                }
            };
            mCountDownTimer.start();
        }
    }

    protected int getWaitTime() {
        return 10000;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detach();
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
    }

    public abstract T getPresenter();
}
